package org.dudemonkey.thread.p128;

import lombok.extern.slf4j.Slf4j;
import org.dudemonkey.jvm.concurrency.util.PrintUtil;

/**
 * p128 笔记
 * 2020-6-19
 * 控制线程，让t2优先于t1执行
 */
@Slf4j
public class ThreadOrder {

    static Object lock = new Object();
    static boolean t2order=false;


    public static void main(String[] args) {

        new Thread(()->{

            synchronized (lock){
                // 判断t2是否已执行
                while (!t2order){
                    log.debug("看标记 t2order [{}] ",t2order);
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("根据标记 t2order [{}] , t2执行完成 ",t2order);
            }
        },"t1").start();

        PrintUtil.sleep(2);

        new Thread(()->{

            synchronized (lock){
                t2order=true;
                lock.notify();
                log.debug("当前线程获得到了锁, 状态 置位[{}]",t2order);
            }
        },"t2").start();

    }
}
